package com.chance.cc.crawler.development.command.job.domain.douyin.api;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.development.scripts.douyin.api.DYCommentCrawlerScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author bx
 * @date 2020/12/6 0006 12:44
 */
public class DYCommentCrawlerScheduleJob {

    private static String domainId = "api-dy";
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);
    private static final String metaServer = "http://192.168.1.215:9599";
    public static final String articleIdsQueue = StringUtils.joinWith("-",  domainId,"article","ids", "queue");
    public static void main(String[] args) {

        publishCrawlerScheduleJobInfo();
        System.out.println(dyCrawlerSchduler().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob dyCrawlerSchduler = dyCrawlerSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(dyCrawlerSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("抖音评论采集");
        crawlerScheduleJob.setCrawlerKey(dyCrawlerSchduler.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return dyCrawlerSchduler;

    }

    /**
     * dy采集
     * @return
     */
    public static CrawlerJob dyCrawlerSchduler() {


        CrawlerRequestRecord startRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(DYCommentCrawlerScript.commentStartUrl)
                .httpHead("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 8.0.0; zh_CN; MI 5s; Build/OPR1.170623.032; Cronet/TTNetVersion:1c8b77ac 2020-12-16 QuicVersion:47946d2a 2020-10-14)")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .build();
        startRequestRecord.setDownload(false);
        startRequestRecord.setSkipPipeline(true);
        startRequestRecord.tagsCreator().bizTags().addDomain(domainId);
        startRequestRecord.tagsCreator().bizTags().addSite(DYCommentCrawlerScript.site);

        CrawlerRequestRecord userOauthInfoCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dy_oauth_infos", turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/oauth/api/v1/douyin/userOauthInfos?fuzzy=device_comment")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerRequestRecord commentRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(DYCommentCrawlerScript.commentUrl)
                .httpHead("User-Agent", "com.ss.android.ugc.aweme/140101 (Linux; U; Android 8.0.0; zh_CN; MI 5s; Build/OPR1.170623.032; Cronet/TTNetVersion:1c8b77ac 2020-12-16 QuicVersion:47946d2a 2020-10-14)")
                .releaseTime(System.currentTimeMillis())
                .needWashed(true)
                .needParsed(true)
                .build();
        commentRequestRecord.tagsCreator().bizTags().addDomain(domainId);
        commentRequestRecord.tagsCreator().bizTags().addSite(DYCommentCrawlerScript.site);
        String initUrl = metaServer + "/crawler/domain/common/api/v1/dy/realtime/comment/queue/init?" +
                "crawlerArticleIdsQueueName=%s&crawlerCommentQueueName=%s";
        String requestQueueName = StringUtils.joinWith("-", "crawler", domainId, DYCommentCrawlerScript.site, "queue");
        CrawlerRequestRecord initRealTimeCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs_comment_init", turnPageItem)
                .httpUrl(String.format(initUrl, articleIdsQueue,requestQueueName))
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
            HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(commentRequestRecord), "utf-8");
        initRealTimeCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initRealTimeCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-","comment", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerJobThreadNumber(3)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .fileResultPipeline(null, "/data/chance_crawler_test/data/douyin/dy_comment.log", false)
                .kafkaResultPipeline(null, "tmp_social_media", null)
                .requestRecord(startRequestRecord)
                .supportRecord(userOauthInfoCrawlerRecord)
                .supportRecord(initRealTimeCommentCrawlerRecord)
                .restWhileNon(false)
                .build();
//        crawlerJob.setInitRequestQueue(false);
        return crawlerJob;
    }

}
